package com.example.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.system.dto.CurUserInvitationInfo;
import com.example.system.entity.Invitation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface InvitationMapper extends BaseMapper<Invitation> {
    void invite(Invitation invitation);

    void update(Invitation invitation);

    @Select("SELECT COUNT(*) " +
            "FROM invitation " +
            "WHERE " +
            "inviter_id = #{inviterId} AND " +
            "invitee_id = #{inviteeId} AND " +
            "(invitation_status = 'no-response-received' OR " +
            "invitation_status = 'accepted')")
    boolean existsNotRejectedUserIdPair(@Param("inviterId") Integer inviterId, @Param("inviteeId") Integer inviteeId);

    @Select("SELECT COUNT(*) " +
            "FROM invitation " +
            "WHERE " +
            "inviter_id = #{inviterId} AND " +
            "invitee_id = #{inviteeId} AND " +
            "invitation_status = 'no-response-received'")
    boolean existsNoResponseReceivedUserIdPair(@Param("inviterId") Integer inviterId, @Param("inviteeId") Integer inviteeId);

    @Select("SELECT * " +
            "FROM invitation " +
            "WHERE " +
            "inviter_id = #{inviterId} AND " +
            "invitee_id = #{inviteeId} AND " +
            "invitation_status = 'no-response-received'")
    Invitation getNoResponseReceivedInvitationByUserIdPair(@Param("inviterId") Integer inviterId, @Param("inviteeId") Integer inviteeId);

    @Select("SELECT invitation_id, invitee_id AS userId, nickname, invitation_status, invitation_time " +
            "FROM invitation " +
            "INNER JOIN user on invitee_id = id " +
            "WHERE " +
            "inviter_id = #{userId}")
    List<CurUserInvitationInfo> getAllInviterRecordsByUserId(@Param("userId") Integer userId);

    @Select("SELECT invitation_id, invitee_id AS user_id, nickname, invitation_status, invitation_time " +
            "FROM invitation " +
            "INNER JOIN user on invitee_id = id " +
            "WHERE " +
            "inviter_id = #{userId} AND " +
            "invitee_id = #{inviteeId}")
    List<CurUserInvitationInfo> getAllInviterRecordsByUserIdAndInviteeId(@Param("userId") Integer userId, @Param("inviteeId") Integer inviteeId);

    @Select("SELECT invitation_id, inviter_id AS user_id, nickname, invitation_status, invitation_time " +
            "FROM invitation " +
            "INNER JOIN user on invitee_id = id " +
            "WHERE " +
            "invitee_id = #{userId} AND " +
            "inviter_id = #{inviterId}")
    List<CurUserInvitationInfo> getAllInviteeRecordsByUserIdAndInviterId(@Param("userId") Integer userId, @Param("inviterId") Integer inviterId);

    @Select("SELECT invitation_id, inviter_id AS userId, nickname, invitation_status, invitation_time " +
            "FROM invitation " +
            "INNER JOIN user on inviter_id = id " +
            "WHERE " +
            "invitee_id = #{userId}")
    List<CurUserInvitationInfo> getAllInviteeRecordsByUserId(@Param("userId") Integer userId);

    @Select("SELECT COUNT(*) FROM invitation WHERE invitee_id = #{userId} AND invitation_status = 'no-response-received'")
    Integer getAllNewInviteeRecordsCountByUserId(@Param("userId") Integer userId);

    @Select("SELECT COUNT(*) FROM invitation WHERE invitation_id = #{invitationId}")
    boolean existsId(@Param("invitationId") Integer invitationId);

    @Delete("DELETE FROM invitation WHERE invitation_id = #{invitationId}")
    void deleteOneRecordByInvitationId(@Param("invitationId") Integer invitationId);
}
